package cn.mumzone.pos.ui.fragment;

import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.LinearLayout;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.SimpleFragment;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.R;
import cn.mumzone.pos.event.MoreViewChangeEvent;
import cn.mumzone.pos.ui.adapter.ADA_ReportFormFragmentPager;
import cn.mumzone.pos.ui.widgets.others.BanScrollViewPager;

/**
 * author:        yupeng
 * created on:    2019/7/10 16:07
 * description:   业务报表
 */
public class FRA_BusinessReportForms extends SimpleFragment {

    @BindView(R.id.iv_back)
    AppCompatImageView ivBack;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    BanScrollViewPager viewPager;

    @Override
    protected void initVariable() {

    }

    @Override
    protected void initEventAndData() {
        viewPager.setOffscreenPageLimit(3);
        ADA_ReportFormFragmentPager pagerAdapter = new ADA_ReportFormFragmentPager(getFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        // 设置指示器宽度
        setUpIndicatorWidth(tabLayout, 200, 150);
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_business_report_forms;
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                RxBus.getDefault().post(new MoreViewChangeEvent(MoreViewChangeEvent.MORE_VIE_HOME));
                break;
        }
    }

    private void setUpIndicatorWidth(TabLayout tabLayout, int marginLeft, int marginRight) {
        Class<?> tabLayoutClass = tabLayout.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        LinearLayout layout = null;
        try {
            if (tabStrip != null) {
                layout = (LinearLayout) tabStrip.get(tabLayout);
            }
            for (int i = 0; i < layout.getChildCount(); i++) {
                View child = layout.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    params.setMarginStart(DisplayUtil.dip2px(getActivity(), marginLeft));
                    params.setMarginEnd(DisplayUtil.dip2px(getActivity(), marginRight));
                }
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
